package com.chenfan.ccp.extend;

import cn.afterturn.easypoi.util.PoiPublicUtil;
import cn.afterturn.easypoi.util.PoiPublicUtils;
import cn.afterturn.easypoi.util.PoiUtil;
import feign.SynchronousMethodHandler;
import com.chenfan.ccp.util.start.ApplicationContextUtil;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;
import net.logstash.logback.appender.AbstractLogstashTcpSocketAppender;

import java.io.File;

public class ThirdLoadExtends {


    public static void init() {

        try {


            ApplicationContextUtil.setProdProfile();

        } catch (Exception e) {

        }

        loadProp();

        try {

            if (Class.forName("cn.afterturn.easypoi.entity.ImageEntity") != null
                    && PoiPublicUtils.class.getMethod("loadData") != null) {
                PoiPublicUtils.loadData();
            }
            // 处理
            dealEasypoi();

        } catch (Exception e) {

        }
        try {
            if (Class.forName("feign.Request.Options") != null &&
                    SynchronousMethodHandler.class.getMethod("loadData") != null) {
                SynchronousMethodHandler.loadData();
            }


        } catch (Exception e) {

        }
        try {
            if (Class.forName("ch.qos.logback.core.util.Duration") != null &&
                    AbstractLogstashTcpSocketAppender.class.getMethod("loadData") != null) {
                AbstractLogstashTcpSocketAppender.loadData();
            }
        } catch (Exception e) {

        }


    }

    public static void main(String[] args) {

    }

    private static void loadProp() {

        if (!ApplicationContextUtil.isProd()) {
            System.setProperty("management.metrics.export.prometheus.enabled", "false");
        }

    }

    private static void dealEasypoi(){
        try {

            Class object = Class.forName("cn.afterturn.easypoi.entity.ImageEntity");
            if (object != null){
                ClassLoader contextClassLoader = PoiUtil.class.getClassLoader();
                System.out.println(contextClassLoader.getClass().getSimpleName());
                ByteBuddyAgent.install();
                ClassReloadingStrategy classReloadingStrategy = ClassReloadingStrategy.fromInstalledAgent();
                new ByteBuddy().redefine(PoiPublicUtil.class)
                        .method(ElementMatchers.named("getFileExtendName"))
                        .intercept(MethodDelegation.to(PoiUtil.class))
                        .make()
                        .load(contextClassLoader, classReloadingStrategy);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static<T> boolean isStartupFromJar(Class<T> clazz) {
        File file = new File(clazz.getProtectionDomain().getCodeSource().getLocation().getPath());
        return file.isFile();
    }




}
